package com.dxy.app.widget.maskdragger_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.dxy.app.R;
import com.dxy.app.widget.ThreadManager;

/**
  *Created by duxingyu on 2017/7/25.9:48
  *Email: 
  *Description:两年前的一个需求，要达到一个遮罩效果，当时不知从何处下手,现在使用clipRect方法便可轻易达到效果
  **/

public class MaskDraggerView extends View {

    public static String TAG="MaskDraggerView";
    /**
     * 是否终止自动回弹的效果
     * */
    private boolean finishRestoreTask =false;
    /**
     * 画笔
     * */
    private Paint paint;
    /**
     * 遮罩的颜色
     * */
    private int COLOR_MASK;
    /**
     * 背景头像
     * */
    private Bitmap backGround;
    /**
     * 控件宽高
     * */
    private int WIDTH,HEIGHT;
    /**
     * 松手后自动还原状态
     * */
    private boolean forwardDown =true;
    /**
     * 一些参数
     * */
    private float y,//当前触发事件的Y轴坐标
        delY,//每次位移的距离
        curY;//当前的基准Y轴坐标

    public MaskDraggerView(Context context) {
        super(context);
    }

    public MaskDraggerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.MaskDraggerView);
        init(array);
    }

    public MaskDraggerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.MaskDraggerView);
        init(array);
    }
    /**
     * 初始化一些参数
     * */
    private void init(TypedArray array){
        if(array!=null){
            int resId =array.getResourceId(R.styleable.MaskDraggerView_mask_resource,R.mipmap.ic_avatar_lawyer_vip);
            backGround =BitmapFactory.decodeResource(getContext().getResources(),resId);
            COLOR_MASK =array.getResourceId(R.styleable.MaskDraggerView_mask_cover_color,R.color.half_yellow);
        }else{
            initDefaultValue();
        }
        initParams();
    }
    /**
     * 初始化默认值
     * */
    protected void initDefaultValue(){
        backGround = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_avatar_lawyer_vip);
        COLOR_MASK =getContext().getResources().getColor(R.color.half_yellow);
    }
    /**
     * 初始化一些变量
     * */
    private void initParams(){
        paint =new Paint();
        paint.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw: "+"sss");
        canvas.drawBitmap(backGround,0,0,paint);
        canvas.clipRect(0,0,WIDTH,curY, Region.Op.INTERSECT);
        paint.setColor(COLOR_MASK);
        if(curY>=0){
            curY+=delY;
        }
        if(curY<0){
            curY=0;
        }
        if(curY>HEIGHT){
            curY =HEIGHT;
        }
        canvas.drawCircle(WIDTH/2,HEIGHT/2,WIDTH/2,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        WIDTH =MeasureSpec.getSize(widthMeasureSpec);
        HEIGHT =MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(WIDTH,HEIGHT);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                delY =0;//避免在处于回弹效果的期间，新的事件到来导致mask的断层式绘制。
                finishRestoreTask =true;
                y =event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float tempY =event.getRawY();
                delY =tempY -y;
                y =tempY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                finishRestoreTask =false;
                float yy =event.getRawY();
                delY =yy-y;
                if(curY+delY>HEIGHT/2){
                    forwardDown =true;
                }else{
                    forwardDown =false;
                }
                restoreState();
                break;
        }
        return true;
    }
    private void restoreState(){
        ThreadManager.getShortPool().execute(()->{
            while(curY>0){
                if(finishRestoreTask){
                    postInvalidate();
                    break;
                }
                if(forwardDown){
                    delY=10;
                }else{
                    delY=-10;
                }
                postInvalidate();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });
    }
}
